import requests
import json
from langchain_text_splitters import MarkdownHeaderTextSplitter
from langchain_core.documents import Document
from uuid import uuid4
from langchain_chroma import Chroma
import html2text
from pathlib import Path


def is_port_open(url):
    try:
        response = requests.get(url)
        return True
    except requests.ConnectionError:
        return False


def get_request_json(url):
    try:
        response = requests.get(url)
        return response.json()
    except:
        return {}


def html2md(html_json_path, md_json_path):
    """playwright 抓取的 html 转换为 markdown"""
    h = html2text.HTML2Text()
    h.ignore_links = False
    h.mark_code = True
    h.escape_all = True
    h.wrap_tables = False
    h.pad_tables = False
    # h.ignore_tables = True
    h.body_width = 0
    h.wrap_links = False
    h.unicode_snob = True

    modified_data = []
    with open(html_json_path, 'r', encoding='utf-8') as file:
        data = json.load(file)
    for item in data:
        new_item = {
            "标题": item['标题'],
            "链接": item['链接'],
            "md": h.handle(item["html"])
        }
        modified_data.append(new_item)
    with open(md_json_path, 'w', encoding='utf-8') as file:
        json.dump(modified_data, file, ensure_ascii=False, indent=1)


def split_md_on_header(markdown_document):
    """按标题拆分 markdown 为更小的块"""
    headers_to_split_on = [
        ("#", "1"),
        ("##", "2"),
    ]

    markdown_splitter = MarkdownHeaderTextSplitter(
        headers_to_split_on, strip_headers=False, return_each_line=False)

    return markdown_splitter.split_text(markdown_document)


def create_res_document(md_json_path):
    docs = []
    with open(md_json_path, 'r', encoding='utf-8') as file:
        md_list = json.load(file)
    for md in md_list:
        if (len(md['md']) < 2500):
            docs.append(Document(page_content=md['md'], metadata={
                        "链接": md['链接'], "标题": md['标题']}))
        else:
            print(md['标题'], "md 长度大于 2500 字符")
            split_md_list = split_md_on_header(md['md'])
            for split_md_item in split_md_list:
                docs.append(Document(page_content=split_md_item.page_content, metadata={
                    "链接": md['链接'], "标题": md['标题']}))
    return docs


def get_book_name(path):
    file_path = Path(path)
    return file_path.stem
